package com.gx.telephonedemo2.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;
import com.gx.telephonedemo2.Constant;
import com.gx.telephonedemo2.MainActivity;
import com.gx.telephonedemo2.R;
import com.gx.telephonedemo2.Utils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        rxPermissionTest();
    }

    @SuppressLint("CheckResult")
    private void rxPermissionTest() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.WRITE_CONTACTS,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.GET_ACCOUNTS,
                Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(granted -> {
            if (!granted) {
                // 权限被拒绝
                Toast.makeText(LoginActivity.this, "请通过所有权限！", Toast.LENGTH_LONG).show();
            }else {
                loadMailList();
            }
        });
    }

    private void loadMailList() {
        showLoadingDialog();
        Observable.create((ObservableOnSubscribe<String>) subscriber -> {
            Constant.guaduanTime = System.currentTimeMillis();
            Constant.blockCalls.clear();
            Constant.blockCalls.addAll(Utils.getPhoneNum(LoginActivity.this));
            subscriber.onNext("hello");
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    hideLoadingDialog();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                });
    }
}
